class Singleton {
  private state: string = "hide";
  // 外部无法初始化
  private constructor() {}
  show() {
    if (this.state === "show") {
      console.log("已显示");
      return;
    }
    console.log("显示");
    this.state = "show";
  }

  hide() {
    if (this.state === "hide") {
      console.log("已隐藏");
      return;
    }
    console.log("隐藏");
    this.state = "hide";
  }
  // 静态属性，用来保存实例
  private static instance: Singleton | null;
  // 静态方法，用来获取实例
  static getInstance(): Singleton {
    // this 指向 Singleton
    if (this.instance == null) {
      console.log("创建实例");
      // Singleton.instance = new Singleton();
      this.instance = new Singleton(); // 静态方法，用来获取实例
    }
    // return Singleton.instance;
    return this.instance;
  }
}

const s1 = Singleton.getInstance();
const s2 = Singleton.getInstance();
console.log(Singleton);
console.log(s1 === s2); // true
s1.show(); // 显示
s1.show(); // 已显示
